//SPDX-FileCopyrightText: Copyright 2022-2024 深圳市同心圆网络有限公司
//SPDX-License-Identifier: GPL-3.0-only

package idea_dao

import (
	"context"

	"atomgit.com/openlinksaas-org/api-server/dao/db_ns"
	"atomgit.com/openlinksaas-org/proto-gen-go.git/idea_store_api"
	"go.mongodb.org/mongo-driver/bson"
	"go.mongodb.org/mongo-driver/mongo"
	"go.mongodb.org/mongo-driver/mongo/options"
)

type IdeaStoreDbItem struct {
	IdeaStoreId string `bson:"_id" json:"_id"`
	StoreCateId string `bson:"storeCateId" json:"storeCateId"`
	Name        string `bson:"name" json:"name"`
	Weight      uint32 `bson:"weight" json:"weight"`
	IdeaCount   uint32 `bson:"ideaCount" json:"ideaCount"`
}

func (item *IdeaStoreDbItem) ToIdeaStore() *idea_store_api.IdeaStore {
	return &idea_store_api.IdeaStore{
		IdeaStoreId: item.IdeaStoreId,
		Name:        item.Name,
		Weight:      item.Weight,
		IdeaCount:   item.IdeaCount,
		StoreCateId: item.StoreCateId,
	}
}

type _IdeaStoreDao struct {
	mc *mongo.Collection
}

func newIdeaStoreDao(db *mongo.Database) (*_IdeaStoreDao, error) {
	mc := db.Collection(db_ns.DB_NS_PROJECT_IDEA + "store")
	_, err := mc.Indexes().CreateOne(context.Background(), mongo.IndexModel{
		Keys: bson.D{{"storeCateId", 1}},
	})
	if err != nil {
		return nil, err
	}
	return &_IdeaStoreDao{mc: mc}, nil
}

func (db *_IdeaStoreDao) Insert(ctx context.Context, dbItem *IdeaStoreDbItem) error {
	_, err := db.mc.InsertOne(ctx, dbItem)
	return err
}

func (db *_IdeaStoreDao) Exist(ctx context.Context, ideaStoreId string) (bool, error) {
	count, err := db.mc.CountDocuments(ctx, bson.M{"_id": ideaStoreId})
	return count > 0, err
}

func (db *_IdeaStoreDao) Get(ctx context.Context, ideaStoreId string) (*IdeaStoreDbItem, error) {
	retItem := &IdeaStoreDbItem{}
	err := db.mc.FindOne(ctx, bson.M{"_id": ideaStoreId}).Decode(retItem)
	return retItem, err
}

func (db *_IdeaStoreDao) Remove(ctx context.Context, ideaStoreId string) error {
	_, err := db.mc.DeleteOne(ctx, bson.M{"_id": ideaStoreId})
	return err
}

func (db *_IdeaStoreDao) CountByCate(ctx context.Context, storeCateId string) (uint32, error) {
	count, err := db.mc.CountDocuments(ctx, bson.M{"storeCateId": storeCateId})
	return uint32(count), err
}

func (db *_IdeaStoreDao) List(ctx context.Context, filterByStoreCateId bool, storeCateId string) ([]*IdeaStoreDbItem, error) {
	query := bson.M{}
	if filterByStoreCateId {
		query["storeCateId"] = storeCateId
	}
	cursor, err := db.mc.Find(ctx, query, options.Find().SetSort(bson.M{"weight": -1}))
	if err != nil {
		return nil, err
	}
	retList := []*IdeaStoreDbItem{}
	err = cursor.All(ctx, &retList)
	return retList, err
}

func (db *_IdeaStoreDao) UpdateNameAndWeight(ctx context.Context, ideaStoreId, name string, weight uint32) error {
	_, err := db.mc.UpdateOne(ctx, bson.M{"_id": ideaStoreId},
		bson.M{"$set": bson.M{"name": name, "weight": weight}})
	return err
}

func (db *_IdeaStoreDao) UpdateIdeaCount(ctx context.Context, ideaStoreId string, count uint32) error {
	_, err := db.mc.UpdateOne(ctx, bson.M{"_id": ideaStoreId},
		bson.M{"$set": bson.M{"ideaCount": count}})
	return err
}

func (db *_IdeaStoreDao) UpdateStoreCate(ctx context.Context, ideaStoreId, storeCateId string) error {
	_, err := db.mc.UpdateOne(ctx, bson.M{"_id": ideaStoreId},
		bson.M{"$set": bson.M{"storeCateId": storeCateId}})
	return err
}
